#include "configdlg.h"
#include "ui_configdlg.h"

#include "configkeys.h"

#include <QSettings>
#include <QDebug>

CConfigDlg::CConfigDlg(QWidget *parent) :
	QDialog(parent),
	m_ui(new Ui::CConfigDlg)
{
	m_ui->setupUi(this);

	connect(this, SIGNAL(accepted()), this, SLOT(onOK()));
	connect(m_ui->buttonBox, SIGNAL(clicked(QAbstractButton*)), this, SLOT(btnCLicked(QAbstractButton*)));

	onLoad();
}

CConfigDlg::~CConfigDlg()
{
	delete m_ui;
}

void CConfigDlg::changeEvent(QEvent *e)
{
	switch (e->type()) {
	case QEvent::LanguageChange:
		m_ui->retranslateUi(this);
		break;
	default:
		break;
	}
}

void CConfigDlg::onOK()
{
	QSettings s;
	qDebug() << "Saving settings to:" << s.fileName();
	s.setValue(K_SERVER_ADDR, m_ui->serverAddressLineEdit->text());
	s.setValue(K_SERVER_PORT, m_ui->serverPortSpinBox->value());
}

void CConfigDlg::onLoad()
{
	QSettings s;
	qDebug() << "Loading settings from:" << s.fileName();
	m_ui->serverAddressLineEdit->setText(s.value(K_SERVER_ADDR, "127.0.0.1").toString());
	m_ui->serverPortSpinBox->setValue(s.value(K_SERVER_PORT, 16000).toInt());
}

void CConfigDlg::btnCLicked(QAbstractButton *pBtn)
{
	if (m_ui->buttonBox->standardButton(pBtn) == QDialogButtonBox::Reset)
		onLoad();
}
